
import $api from "@/api/index.js"
import $i18n from "@/utils/i18n.js" // 多语言
export default {
	state: {
		activeSort: 0,
		sortTab:[{
			name:	$i18n.tc('index.subnav.rand'),
			sort: 	'rand'
		},{
			name: 	$i18n.tc('index.subnav.count'),
			sort: 	'read_count'
		},{
			name: 	$i18n.tc('index.subnav.price'),
			sort: 	'unit_price'
		},{
			name: 	$i18n.tc('index.subnav.id'),
			sort: 	'id'
		}],
		cateList:[],
		catSelect:'',
		indexParam: {
			key: 'indexList',
			type: 1,
			page: 1,
			sort: '',
			state: 3,
			limit_surplus_num:1,
			limit_end_time:1,
		},
		indexList: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		taskSortIndex: 0,
		taskCateId:'',
		taskParam: {
			key: 'taskList',
			type: 1,
			page: 1,
			sort: '',
			category_id: '',
			state: 3,
			limit_surplus_num:1,
			limit_end_time:1,
		},
		taskList: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		searchParam: {
			page: 1,
			state: 3,
			limit_surplus_num:1,
			title:''
		},
		topParam: {
			type: 1,
			state: 3,
			is_top: 1,
			limit_surplus_num:1,
			limit_end_time:1,
		},
		topList:[],
		topTask:[],
		myTaskParam: {
			page: 1,
			state: '',
			channel:2,
			channel_id:''
		}, // 我发布的任务参数
		myTaskList: {
			data: [],
			last_page: 1,
			current_page: 1
		}, // 我发布的任务列表
		collectList: {
			data: [],
			last_page: 1,
			current_page: 1
		}, // 我的收藏
		collectParam: {
			page: 1,
		},
		taskTrends:[]
		// topList:[],
	},
	mutations: {
		updateTaskState(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
			if(key == 'catTab'){
				uni.setStorageSync('taskCatTab', val)
			}
		}
		
	},
	actions: {
		async getTaskList({
			commit,
			state
		}, param) {
			let d = await $api.task.getTaskList(param)
			let oldList = state[param.key];
			let newList = d.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateTaskState', {
				key: param.key,
				val: list
			})
		},
		
		async getTaskCate({
			commit,
			state
		},param){
			let res = await $api.task.getTaskCate()
			let cate = []
			res.data.forEach((cat) => {
				let {
					id,
					title,
					thumb,
					sort
				} = cat
				cate.push({
					name:title,
					thumb:thumb,
					cid:id,
				});
			})
			commit('updateTaskState', {
				key: 'cateList',
				val: cate
			})
			// 	catTab.push({
			// 		name:title,
			// 		thumb:thumb,
			// 		cid:id,
			// 	});
			// let catTab = [{
			// 	name: $i18n.tc('task.all'),
			// 	thumb:'',
			// 	cid:0,
			// }]
			// let catSelect = []
			// res.data.forEach((cat) => {
			// 	let {
			// 		id,
			// 		title,
			// 		thumb,
			// 		sort
			// 	} = cat
			// 	catTab.push({
			// 		name:title,
			// 		thumb:thumb,
			// 		cid:id,
			// 	});
			// 	catSelect.push({
			// 		name:title,
			// 		cid:id,
			// 	});
			// })
			
			// let data = Object.assign(state.catTab, catTab)
			// commit('updateTaskState', {
			// 	key: 'catTab',
			// 	val: data
			// })
			
			// // let data1 = Object.assign(state.catSelect, catSelect)
			// // console.log(data1)
			// commit('updateTaskState', {
			// 	key: 'catSelect',
			// 	val: catSelect
			// })
		},
		
		// 获取置顶任务
		async getTopTaskList({
			commit,
			state
		},param){
			// getTopList
			let res = await $api.task.getTopTask(param)
			commit('updateTaskState', {
				key: 'topList',
				val: res.data
			})
		},
		
		// 获取我发布的任务
		async getMyTaskList({
			commit,
			state
		}, param) {
			let res = await $api.task.getTaskList(param)
			let oldList = state.myTaskList;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateTaskState', {
				key: 'myTaskList',
				val: list
			})
		},
		
		// 获取我收藏的任务
		async getMyCollectList({
			commit,
			state
		},param){
			let res = await $api.task.getMyCollectList(param)
			let oldList = state.collectList;
			let newList = res.data;
			let list = {}
			if (param.page == 1) {
				list = newList;
			} else {
				newList.data = oldList.data.concat(newList.data) // 合并数组
				list = newList;
			}
			commit('updateTaskState', {
				key: 'collectList',
				val: list
			})
		},
		
	}
}